<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标事件</title>
    <style>
        .box{
            width: 500px;
            height: 400px;
            border:solid 1px #ddd;
            margin: 100px auto;
            position:relative;
        }
        .inner{
            width: 150px;
            height: 150px;
            border:solid 1px #999;
            position:absolute;
        }
    </style>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            state = {
                isDown: false
            }

            render(){
                return <div className="box" ref={el => this.box = el} onClick={this.change}>
                        <a href="http://www.atguigu.com" onClick={this.jump}>尚硅谷</a>
                        <div className="inner" ref={el => this.inner = el} 
                            onClick={this.innerClick} 
                            onMouseMove={this.move}
                            onMouseDown={this.down}
                            onMouseUp = {this.up}
                            >
                        </div>
                    </div>
            }

            change = (e) => {
                //获取事件源
                // console.log(e.target);
                e.target.style.background = '#cae';
            }

            jump = (e) => {
                //阻止默认行为
                e.preventDefault();
                //这个无法阻止默认行为
                // return false;
            }

            innerClick = (e) => {
                //阻止冒泡
                e.stopPropagation();
                //获取鼠标的位置
                console.log(e);
            }

            down = (e) => {
                this.x = e.clientX;
                this.l = this.inner.offsetLeft;
                this.setState({
                    isDown: true
                })
            }

            move = (e) => {
                if(!this.state.isDown) return;
                let _x = e.clientX;
                //计算
                let newLeft = _x - this.x + this.l;
                //设置
                this.inner.style.left = newLeft + 'px';
            }

            up = () => {
                //????
                this.setState({
                    isDown: false
                })
            }




            move_bak = (e) => {
                //获取外层元素距离窗口左侧的偏移
                let one = this.box.offsetLeft;
                let oneV = this.box.offsetTop;
                //鼠标距离左侧的偏移
                let three = e.clientX;
                let threeV = e.clientY;
                //
                let two = this.inner.offsetWidth / 2;
                let twoV = this.inner.offsetHeight /2;
                //计算 left
                let newLeft = three - one - two;
                if(newLeft <=0) newLeft = 0;
                let newTop = threeV - oneV - twoV;
                this.inner.style.left = newLeft + 'px';
                this.inner.style.top = newTop + 'px';
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>